package com.nsl.my_youlai.Jackson;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import enums.learn.ColorEnum;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class ColorEnumSerializer extends StdSerializer<ColorEnum> {
    public ColorEnumSerializer() {
        this(null);
    }

    protected ColorEnumSerializer(Class<ColorEnum> vc) {
        super(vc);
    }
    @Override
    public void serialize(ColorEnum colorEnum, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if (colorEnum == null) {
            jsonGenerator.writeNull();
            return;
        }
        Map<String,Object> map=new HashMap<>();

        map.put("value",colorEnum.getValue());
        map.put("label",colorEnum.getLabel());
        ObjectMapper objectMapper=new ObjectMapper();

        jsonGenerator.writeString(objectMapper.writeValueAsString(map));
    }
}
